<template>
  <div class="container" style="height: 100vh;width: 300px;padding: 20px;box-sizing: border-box;width: 100%;">
    <header>
      <MBreadcrumb :item="breadcrumbItem" />
    </header>
    <main>
      <MTitle class="title">
        <template #left>
          调度信息
        </template>
      </MTitle>
      <table id="attributeTable" class="mtable">
        <tr class="mtr">
          <td class="field">在库司机</td>
          <td class="value">
            <MInput v-model="ProductInfo[0]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">电话</td>
          <td class="value">
            <MInput v-model="ProductInfo[1]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">车辆</td>
          <td class="value">
            <MInput v-model="ProductInfo[2]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">车牌</td>
          <td class="value">
            <MInput v-model="ProductInfo[3]" style="font-size: 12px;width: 100%" disabled>
            </MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">目的地</td>
          <td class="value">
            <MInput v-model="ProductInfo[4]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">用车时间</td>
          <td class="value">
            <MInput v-model="ProductInfo[5]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
      </table>
      <MTitle class="title">
        <template #left>
          上报信息
        </template>
      </MTitle>
      <table id="attributeTable" class="mtable">
        <tr class="mtr">
          <td class="field">司机上报</td>
          <td class="value">
            <MInput v-model="ProductInfo[6]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">车辆上报</td>
          <td class="value">
            <MInput v-model="ProductInfo[7]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
      </table>
    </main>
    <footer>
      <div style="text-align: center;" v-if="!isDisable">
        <MButton @click="departCar">发车</MButton>
      </div>
    </footer>
    <MMessage v-if="toastVisible" :message="toastMessage" :state="toastState" />

  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import MBreadcrumb from "../../components/MBreadcrumb.vue"
import MTitle from "../../components/MTitle.vue"
import MInput from "../../components/MInput.vue"
import MMessage from '../../components/MMessage.vue';
import MButton from '../../components/MButton.vue';
import axios from "../../axios/axios"
export default {
  name: "PartOrder",
  components: {
    MBreadcrumb,
    MTitle,
    MButton,
    MInput,
    MMessage,
  },
  setup() {
    const toastVisible = ref(false)
    const toastMessage = ref("")
    const toastState = ref("success")
    const breadcrumbItem = ref(
      {
        key: "0",
        title: '赋码管理',
        children: {
          key: "0-0",
          title: '调度信息',
        }
      })

    const ProductInfo = ref(["", "", "", "", "", ""])
    const tagItems = ref([])
    const params = ref([])
    const userId = ref("")
    const DriverOptions = ref([])
    const isDisable = ref(false)

    const departCar = () => {
      if (ProductInfo.value[6] !== "" && ProductInfo.value[7] !== "") {
        axios.get("/step/sendstep?type=departCar&userId=" + params.value.get('userId') + "&trainingId=" + params.value.get('trainingId'))
        messageShow("已发车", "success")

      } else {
        messageShow("请先上报信息", "warning")
      }
    }
    const messageShow = (msg, state) => {
      toastMessage.value = msg;
      toastVisible.value = true;
      toastState.value = state
      // 使用 setTimeout 模拟 Toast 组件内部的定时器功能（仅用于演示）
      // 在实际使用中，你应该依赖 Toast 组件内部的定时器
      setTimeout(() => {
        toastVisible.value = false;
      }, 3000); // 与 Toast 组件中的 duration 属性保持一致
    }
    onMounted(() => {
      // 获取 URL 参数
      const param = new URLSearchParams(window.location.search);
      params.value = param
      if (param.has('userId')) {
        userId.value = param.get('userId');
      }
      if (param.has('isDisable')) {
        isDisable.value = param.get('isDisable') === "true"
      }
      if (param.has('code')) {
        axios.get('/dispatch/select?logisticsNumber=' + param.get('code'))

          .then(res => {
            var data = []
            data[0] = res.data.Data.driver
            data[1] = res.data.Data.driverphone
            data[2] = res.data.Data.carmodel
            data[3] = res.data.Data.licenseplate
            data[4] = res.data.Data.recipientaddress
            data[5] = res.data.Data.usetime
            data[6] = res.data.Data.driverreport === "1" ? res.data.Data.driver : ""
            data[7] = res.data.Data.carreport === "1" ? res.data.Data.licenseplate : ""
            ProductInfo.value = data
          })
      }
    });
    return {
      toastVisible,
      toastMessage,
      toastState,
      breadcrumbItem,
      tagItems,
      ProductInfo,
      DriverOptions,
      isDisable,
      departCar
    };
  },
};
</script>

<style scoped>
.title {
  margin-top: 30px;
}

.title1 {
  margin-top: 20px;
}

.mtr {
  display: flex;
  flex-wrap: wrap;
  /* 允许子元素换行 */
  align-items: flex-start;
}

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

footer {
  text-align: center;
  height: 10%;
}

main {
  flex: 1;
}

.mtable {
  width: 100%;
  font-size: 13px;

}

.field {
  text-align: left;
  padding: 4px 0;
  flex: 1 1 60px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  /* 确保内容可以缩小 */
  height: 26px;
  line-height: 26px;
}

.value {
  text-align: right;
  padding: 4px 0;
  flex: 1 1 190px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  height: 26px;
  line-height: 26px;
  /* 确保内容可以缩小 */
}
</style>
